<?php

namespace app\user\logic;

use app\model\PigDailyEat;

class PigDailyEatLogic
{
    /**
     * @Description 统计
     * @author: zj
     * @param array $param
     * @time 2024/03/28 10:36:05
     * @return array
     */
    public function statistic(array $param)
    {
        $factory_id = request()->user_info['factory_id'];

        $model = new PigDailyEat();
        $startTime = strtotime(date('Y-m-d 00:00:00',strtotime($param['record_time_start'])));
        $endTime = strtotime(date('Y-m-d 00:00:00',strtotime($param['record_time_end'])));
        $days = ceil(($endTime - $startTime)/86400);

        $data = [];
        for($i = 0;$i<=$days;$i++) {
            $start_time = $startTime + $i * 86400;
            $end_time = $start_time + 86399;
            $feedWeight = $model->where('factory_id',$factory_id)
                ->where('out_pig_id',$param['pig_id'])
                ->where('record_time','>=',$start_time)
                ->where('record_time','<=',$end_time)
                ->where('delete_time',$model::IS_DELETE_NO)
                ->sum('feed_weight') ?? 0;

            $data[] = [
                'date' => date('Y-m-d',$start_time),
                'feed_weight' => $feedWeight
            ];
        }

        return $data;
    }

}